// pages2/pages/authentication/index.js
const $api = require('../../../../request/api.js').API;
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
import * as Storage from '../../../../utils/storage';
import { selectImg, validate } from '../../../../utils/util';
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userId: '',
    accountInfoId: '',
    fullName: '',
    phone: '',
    idCard: '',
    idCardFront: '',
    idCardFrontPath: '',
    idCardBack: '',
    idCardBackPath: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userId: wx.getStorageSync(Storage.USER_ID)
    });
    this.getAccountInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},
  async getAccountInfo() {
    const res = await $api.accountInfo({ userId: this.data.userId });
    const data = res.data.data;
    if (data) {
      const { fullName, phone, idCard, idCardFront, idCardBack, accountInfoId } = data;
      this.setData({
        fullName,
        phone,
        idCard,
        idCardFront,
        idCardBack,
        idCardFrontPath: idCardFront,
        idCardBackPath: idCardBack,
        accountInfoId
      });
    }
  },
  /**
   *  输入
   */
  handleInput(e) {
    const { prop } = e.currentTarget.dataset;
    this.setData({
      [prop]: e.detail
    });
  },
  /**
   *删除
   */
  handleDelete(e) {
    const { prop } = e.currentTarget.dataset;
    this.setData({
      [prop]: ''
    });
  },
  //  选择提现方式
  // hansleCashType(e){
  //   this.setData({
  //     cashType:e.currentTarget.dataset.type
  //   })
  // },
  // 选择图片
  async handleChooseImg(e) {
    const { prop } = e.currentTarget.dataset;
    const { tempPath, baseCode } = await selectImg();
    const path = prop + 'Path';
    this.setData({
      [prop]: baseCode,
      [path]: tempPath
    });
  },
  async submit() {
    const rules = [
      {
        key: 'fullName',
        msg: '请输入真实姓名'
      },
      {
        key: 'phone',
        msg: '请输入手机号',
        pattern: [{ reg: /(^1(3|4|5|6|7|8|9)\d{9}$)|(^0\d{2,3}-?\d{7,8}$)/, msg: '手机号格式错误' }]
      },
      {
        key: 'idCard',
        msg: '请输入身份证号',
        pattern: [{ reg: /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/, msg: '身份证格式错误' }]
      },
      {
        key: 'idCardFront',
        msg: '请上传身份证正面照'
      },
      {
        key: 'idCardBack',
        msg: '请上传身份证反面照'
      }
    ];
    if (!validate(rules, this.data)) return;
    await Dialog.confirm({
      title: '提示',
      message: '确定提交'
    });

    const { fullName, phone, idCard, idCardFront, idCardBack, userId, accountInfoId } = this.data;
    const params = {
      userId,
      fullName,
      phone,
      idCard,
      idCardFront,
      idCardBack,
      userType: 3
    };
    if (accountInfoId) params.accountInfoId = accountInfoId;
    Toast.loading({
      message: '提交中...',
      duration: 0,
      mask: true,
      forbidClick: true
    });
    // 更新
    await $api.setAccountInfo(params);
    Toast.success({
      message: '提交成功',
      forbidClick: true
    });
    const page = getCurrentPages().find(item => item.route == 'pages/person/index');
    if (page) {
      page.setData({
        isAuth: true
      });
    }
    setTimeout(() => {
      wx.navigateBack({
        delta: 1
      });
    }, 2000);
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {}
});
